<?php

	/**
	 * 商品类型管理控制器
	 */
	class TypeAction extends CommonAction{
		
		//商品类型视图
		public function index(){
			import('ORG.Util.Page');
			$db = M('type');
			$count = $db->count('id');
			$page = new page($count , 10);
			$limit = $page->firstRow . ',' .$page->listRows;
			$data = $db->limit($limit)->select();
			$this->page = $page->show();
			$this->data = $data;
			$this->display();
		}

		//添加商品类型视图
		public function addShow(){

			$this->display();
		}

		//添加商品类型到数据库
		public function addType(){
			if($this->isPost()){
				$data = array(
				'name'=>$this->_post('typename'),
				'description'=>$this->_post('description')
				);
				$db = M("type");
				if($db->data($data)->add()){
					$this->success('添加商品类型成功!',U('index'));
				}else{
					$this->error('添加商品类型失败，请重试！');
				}
			}
		}

		//类型属性列表视图
		public function attrList(){
			import('ORG.Util.Page');
			$tid = (int)$_GET['tid'];
			$db = M("type_attr");
			$where = array('tid' => $tid);
			$type = $db->table('hd_type')->where("id='".$tid."'")->field('id,name')->find();
			$count = $db->where($where)->count();
			//数据列表分页
			$page = new page($count , 10);
			$limit = $page->firstRow . ',' . $page->listRows;
			$data = $db->where($where)->limit($limit)->select();
			//分配数据到视图
			$this->page = $page->show();
			$this->type = $type;
			$this->count = $count;
			$this->data = $data;
			$this->display();
		}

		//添加类型属性
		public function addAttr(){
			$this->display();
		}

		//添加类型属性到数据库
		public function runAddAttr(){
			$data = array(
				'name' => $this->_post('typename'),
				'type' => $this->_post('type','intval'),
				'value' => str_replace(PHP_EOL, ',', $_POST['description']),
				'tid' =>$this->_post('tid','intval')
				);
			$db = M('type_attr');
			if($db->data($data)->add()){
				$this->success("添加类型属性成功！",U('attrList',array('tid'=>$data['tid'])));
			}else{
				$this->error("添加类型属性失败，请重试！");
			}

		}


	}
?>